package com.nanshenycd.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nanshenycd.dao.IpDao;
import com.nanshenycd.dao.ViewDao;
import com.nanshenycd.pojo.Blog;
import com.nanshenycd.pojo.Ip;
import com.nanshenycd.pojo.User;
import com.nanshenycd.pojo.View;
import com.nanshenycd.service.BlogService;
import com.nanshenycd.service.TagService;
import com.nanshenycd.service.TypeService;
import com.nanshenycd.vo.BlogQuery;
import com.nanshenycd.vo.SearchBlog;
import com.nanshenycd.vo.ShowBlog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@Api(description  = "后台博客编辑接口")
@RequestMapping("/admin")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private ViewDao viewsDao;
    @Autowired
    private IpDao ipDao;

    //显示所有的博客
    @GetMapping("/blogs")
    @ApiOperation(value = "显示所有的博客")
    public String listBlog(Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //把所有的分类查询出来传到前端选择框中
        model.addAttribute("types", typeService.listType());
        PageHelper.startPage(pageNum, 5);

        List<BlogQuery> blogs = blogService.getAllBlogByBlogQuery();

        //得到分页结果对象
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogs);

        //将查询出来的每页的数据封装到model中
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
        return "admin/blogs";
    }

    //返回查询后的片段信息
    @PostMapping("/blogs/search")
    @ApiOperation(value = "返回查询后的博客片段信息")
    public String search(SearchBlog searchBlog,Model model, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        //将recommend转换一下
        blogService.transformRecommend(searchBlog);
        List<BlogQuery> blogBySearch = blogService.getBlogBySearch(searchBlog);

        PageHelper.startPage(pageNum, 5);

        //得到分页结果对象
        PageInfo<BlogQuery> pageInfo = new PageInfo<>(blogBySearch);

        //将查询出来的每页的数据封装到model中
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());

        return "admin/blogs";
    }

    //跳转新增页面
    @GetMapping("/blogs/input")
    @ApiOperation(value = "跳转新增博客页面")
    public String toAdd(Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    //新增
    @PostMapping("/blogs")
    @ApiOperation(value = "新增")
    public String add(Blog blog, RedirectAttributes attributes, User user, HttpServletRequest request) {
        //设置博客关联session
//        blog.setUser((User) session.getAttribute("user"));
        System.out.println(user);
        blog.setUser(user);

        try {
            //分类初始化 根据前端传输的id封装为一个对象传到blog里面
            blog.setType(typeService.getTypeById(blog.getType().getId()));
            //设置blog中的typeId属性
            blog.setTypeId(blog.getType().getId());
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        //前端传过来的是一个由id拼接的字符串
        blog.setTags(tagService.listTagByIds(blog.getTagIds()));

        //设置用户id
        blog.setUserId(blog.getUser().getId());

        //同步views表
        View view = new View();
        view.setBlogId(blog.getId());
        view.setCount(0L);//浏览量初始化
        viewsDao.insertViews(view);
        //进行ip的封装
        String remoteAddr = request.getRemoteAddr();
        ipDao.saveIp(new Ip(new Date(),remoteAddr));

        int b= blogService.saveBlog(blog);

        if (b <= 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }

    //删除
    @GetMapping("/blogs/{id}/delete")
    @ApiOperation(value = "根据id删除博客")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/blogs";
    }

    //将数据返回给编辑页面
    @GetMapping("/blogs/{id}/input")
    @ApiOperation(value = "继续编辑博客页面接口,将数据返回给编辑页面")
    public String toUpdate(@PathVariable Long id,Model model){
        Blog blogById = blogService.getBlogById(id);
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());

        model.addAttribute("blog",blogById);

        return "admin/blogs-update";
    }


    @PostMapping("/blogs/update")
    @ApiOperation(value = "博客编辑提交请求接口")
    public String update(ShowBlog blog, RedirectAttributes attributes){
        int i=blogService.updateBlog(blog);
        if (i <= 0) { //如果没保存进去给出提示
            attributes.addFlashAttribute("message", "保存失败");
        } else {
            attributes.addFlashAttribute("message", "保存成功");
        }
        return "redirect:/admin/blogs";
    }


}


